from ivic.core import Config, project_path
from ivic.core import Logging
from ivic.vmc.Server import ServerClass

logger = Logging.get_logger('ivic.vmc.Plugin')

__obj = None

def init(daemon_config):
    config = Config.load(project_path('/etc/ivic/vmc.yaml'))
    general_config =  Config.load(project_path('/etc/ivic/general.yaml'))
    config["portal_url"] = general_config["portal_url"]
    # this should dispatch 'ivic.vmc.*' to individual log file
    Logging.get_logger('ivic.vmc', filename = config['log_file'])

    logger.debug('initializing %s' % __name__)
    global __obj
    if __obj:
        logger.error('reinitialized is not supported')
        return

    #try:
    #    external_address = daemon_config['external_address']
    #except Exception, e:
    #    logger.fatal('unable to get external_address from daemon: %s' % str(e))

    __obj = ServerClass(config)

def fini():
    global __obj 
    del __obj

def soap_mods():
    global __obj
    return {'vmc': __obj}

def wsgi_mods():
    return {}
